﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FastDFS.Client;
using log4net;
using log4net.Config;

namespace ConsoleApplication1
{
    class Program
    {

        /// <summary>
        /// log对象，指向日志级别
        /// </summary>
        private static readonly ILog _logger = LogManager.GetLogger("loginfo");
        static void Main(string[] args)
        {

            #region redis队列
            string input = Console.ReadLine();
            Redis.Utils.RedisClient redis = new Redis.Utils.RedisClient();
            redis.RightPush("ErrorCallback", input);
            #endregion

            #region FastDFS
            //Initial
            List<IPEndPoint> trackerIPs = new List<IPEndPoint>();
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.2.50"), 22122);
            trackerIPs.Add(endPoint);
            ConnectionManager.Initialize(trackerIPs);
            StorageNode node = FastDFSClient.GetStorageNode("fdfsgroup1");

            //上传文件
            byte[] content = null;
            if (File.Exists(@"D:\115204240.jpg"))
            {
                FileStream streamUpload = new FileStream(@"D:\115204240.jpg", FileMode.Open);
                using (BinaryReader reader = new BinaryReader(streamUpload))
                {
                    content = reader.ReadBytes((int)streamUpload.Length);
                }
            }

            string fileName = FastDFSClient.UploadFile(node, content, "jpg");
            Console.WriteLine(fileName);

            //流下载
            byte[] buffer = FastDFSClient.DownloadFile(node, fileName);
            using (FileStream stream = new FileStream(@"D:\zzl1.jpg", FileMode.Create))
            {
                using (BinaryWriter write = new BinaryWriter(stream, Encoding.BigEndianUnicode))
                {
                    write.Write(buffer);
                }
            }

            //http下载
            WebClient web = new WebClient();
            web.DownloadFile("http://192.168.2.50/fdfsgroup1/" + fileName, "C:\\abc.jpg");
            #endregion


            //指向log4net.config配置文件
            XmlConfigurator.Configure(new FileInfo(Environment.CurrentDirectory + @"\log4net.config"));
            if (_logger != null)//这块log4net做的不好，需要在调用方写这个判断，太啰嗦了
                _logger.Info("应用程序启动！");

            Hashtable ht = Hashtable.Synchronized(new Hashtable());
            ht.Add("ok", null);
            Console.WriteLine(ht["ok"]);

            Dictionary<string, string> dic = new Dictionary<string, string>();

            Console.ReadKey();
        }
    }
}

